/**
*   \brief  sphereplane.h
*   \author Dave Reid
*   \brief  Header file for checking intersections of a sphere and a plane.
*/
#ifndef __EAGL_INTERSECT_SPHEREPLANE_H_
#define __EAGL_INTERSECT_SPHEREPLANE_H_

#include "../types.h"

namespace eagl
{

/**
*   \brief  Structure for the intersection result between a sphere and a plane.
*/
struct INTERSECT_RESULT_SPHEREPLANE
{
    /// Determines if the sphere is fully inside the halfspace.
    bool fullyInside;
};


/**
*   \brief                   Determines if a sphere and a plane intersect.
*   \param  radius      [in] The radius of the sphere.
*   \param  spherePos   [in] The position of the sphere.
*   \param  planeNormal [in] The normal of the plane.
*   \param  planeDist   [in] The distance of the plane from the origin.
*   \return                  True if the sphere intersects the plane.
*
*   \remarks
*       This function will return true when the sphere is partially or wholly contained
*       in the planes halfspace.
*/
inline bool intersect_sphereplane(float radius, const vec3 &spherePosition,
                                  const vec3 &planeNormal, float planeDist,
                                  INTERSECT_RESULT_SPHEREPLANE *result = NULL)
{
    // First we need the distance between the plane and the position of the sphere.
    float dist = glm::dot(spherePosition, planeNormal) - planeDist;

    // Now we can determine if the sphere is fully inside the half-space.
    if (result != NULL)
    {
        if (dist < -radius)
        {
            result->fullyInside = true;
        }
        else
        {
            result->fullyInside = false;
        }
    }

    return dist <= radius;
}

}

#endif  // __EAGL_INTERSECT_SPHEREPLANE_H_